
var RoleList = new Pearlsen.BaseList({
	gridId:'roleGrid',
	advSearchId:'advSearchWin',
	module:'角色',
	modulePath:'/pages/sysmgr/role/',
	moduleName:'Role',
	idProperty:'ROLE_ID',
	nameProperty:'ROLE_NAME',
	dataSetHandler:'SysRoleDataSetHandler',
	queryHandler:'SysRoleLoadHandler',
	
	statusRender:function(value){
        var val = "";
        switch (value) {
            case "1":
                val = "<span style='color:green;'>启用</span>";
                break;
            case "0":
                val = "<span style='color:red;'>停用</span>";
                break;
        }
        return val;
	},
	authorize:function(){
		var me = RoleList ;
		var record= me.getSelectedOne();
		if( record ){
			var condition=new Condition("roleId",record.get('ROLE_ID'));
			var menuTreeInRoleStroe =Ext.getCmp("menuTreeInRole").getStore();
			var menuTreeNotInRoleStore =Ext.getCmp("menuTreeNotInRole").getStore();
			menuTreeInRoleStroe.clearCondition();
			menuTreeInRoleStroe.addCondition(condition);
			menuTreeInRoleStroe.load();
			menuTreeNotInRoleStore.clearCondition();
			menuTreeNotInRoleStore.addCondition(condition);
			menuTreeNotInRoleStore.load();		
			var roleMenuWin = Ext.getCmp("roleMenuWin");
			roleMenuWin.setTitle("给【"+record.get('ROLE_NAME')+"】授权"  );
			Ext.getCmp("roleMenuWin").show();
		}else{
			Pearlsen.Utils.showErrorMsg("请先选择一个角色");
		}
	},
	assignMenu:function(){
		var me = RoleList ;
		var fromTree = Ext.getCmp("menuTreeNotInRole");
		var toTree = Ext.getCmp("menuTreeInRole");
		me._copyNodeFormTo(fromTree,toTree);
	},
	canclAssignMenu:function(){
		var me = RoleList ;
		var fromTree = Ext.getCmp("menuTreeInRole");
		var toTree = Ext.getCmp("menuTreeNotInRole");
		me._copyNodeFormTo(fromTree,toTree);
	},
	_copyNodeFormTo:function(fromTree,toTree){
		var me = RoleList ;
		var selecteds = fromTree.getChecked();
		for( var i=0 ; i < selecteds.length;i++ ){
			var record = selecteds[i];
			var tempRecord = record;
			var newNodeList= new Array();
			var tempNode = toTree.getStore().getNodeById(record.data.id);
			while(tempNode==null){
			//	tempRecord.removeAll();
				tempRecord.data.children=[];
				newNodeList[newNodeList.length] =tempRecord;
				tempRecord = tempRecord.parentNode ;
				if( tempRecord ){
					tempNode = toTree.getStore().getNodeById( tempRecord.data.id);
				}else{
					break;
				}
			}
			
			for(var j=newNodeList.length-1;j>=0;j-- ){
				var newnode = newNodeList[j].copy();
				newnode.removeAll();
				
				var id = newnode.data.id;
				var parentId = newnode.data.parentId;
				//添加到右边树
				var parentNode = toTree.getStore().getNodeById(parentId);
				parentNode.appendChild(newnode);
				parentNode.expand();
			}

			var oldNode = fromTree.getStore().getNodeById(record.data.id);
			me._deleteNode(oldNode);
		}
	},
	
	_deleteNode:function(node){
		var me = RoleList ;
		if( !node ){
			return ;
		}
		var parentNode=null;
		if(node.parentNode ){
			parentNode = node.parentNode;
		}
		if( node && !node.isRoot() && node.childNodes.length==0){
			node.remove();
		}
		if(  parentNode && !parentNode.isRoot() && parentNode.childNodes.length==0){
			me._deleteNode(parentNode);
		}
	},
	getNodeId:function(nodeIds,node){
		var me = RoleList ;
		if( node.data.id !="0"){
			nodeIds=nodeIds+node.data.id+",";
		}

		for(var i=0;i<node.childNodes.length;i++){
			var child = node.childNodes[i];
			nodeIds=me.getNodeId(nodeIds,child);
		}

		return nodeIds;
	},
	saveRoleMenu:function(){
		var me = RoleList ;
  		var grid=me.getGrid();
		var record= grid.getSelectionModel().getLastSelected();
		var MENU_IDS ="";
		var treeStore =Ext.getCmp("menuTreeInRole").getStore();
		var root = treeStore.getRootNode();
		MENU_IDS=me.getNodeId(MENU_IDS,root);
		var ds=new DataSet('SysRoleDataSetHandler','SysRoleDataSetHandler');
      	ds.setHandlerMethod("saveRoleMenu");
      	ds.addCondition("ROLE_ID",record.get('ROLE_ID'));
      	ds.addCondition("MENU_IDS",MENU_IDS);
		ds.execute(me.afterSaveRoleMenu);
	},
	afterSaveRoleMenu:function(result){
		if( result.success ){
			Pearlsen.Utils.showInfoMsg("角色授权成功!");
			Ext.getCmp("roleMenuWin").hide();			
		}else{
			Pearlsen.Utils.showErrorMsg("出错了,原因:"+result.message);
		}
	},
	cancleSaveRoleMenu:function(){
		Ext.getCmp("roleMenuWin").hide();			
	}
});

